package CTRLayer;
import ModelLayer.*;
import DBLayer.DBOrder;
import DBLayer.DBPartOrder;

public class OrderCTR 
{
	private PartOrderCTR partOrderCTR;
	private DBOrder dbOrder;
	private Order order;
	
	public OrderCTR()
	{
		dbOrder = new DBOrder();
		partOrderCTR = new PartOrderCTR();
	}
	
	public Order createOrder(String orderDate)
	{
		order = new Order(orderDate);
//		if(dbOrder.findSalesOrder(Integer.toString(order.getOrderNO()), false) != null)
//		{
//			boolean done = false;
//			int i = 0;
//			int orderNO = order.getOrderNO();
//			while(!done)
//			{
//				order.setOrderNO(orderNO + i);
//				i++;
//			}
//		}
		return order;
	}
	
	public int insertOrder(String orderDate, String customerPhone, String salesmanID)
	{
		createOrder(orderDate);
		return dbOrder.insertSalesOrder(order, customerPhone, salesmanID);
	}
	
	public Order getOrder(String orderNO, boolean retrieveAssociations)
	{
		Order order = dbOrder.findSalesOrder(orderNO, retrieveAssociations);
		return order;
	}
	
	public void updateOrder(String orderNO, String newCustomerPhone, String newSalesmanID, String newOrderDate)
	{
		Order order = getOrder(orderNO, true);
		order.setOrderDate(newOrderDate);
		dbOrder.updataSalesOrder(order, orderNO, newCustomerPhone, newSalesmanID);
	}

	
	public void deleteOrder(String orderNO)
	{
		if(dbOrder.findSalesOrder(orderNO, true).getPartOrders().size() != 0)
		{
			partOrderCTR.removeAssociatedPartOrders(orderNO);
		}
		dbOrder.removeSalesOrder(orderNO);
	}
}
